package LinkList;

import java.util.List;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: LinkList
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-01  21:47
 * @Description: TODO
 * @Version: 1.0
 */
public class 移除链表中指定的元素203 {
    class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode removeElements3(ListNode head, int val) {
        ListNode dumpy=new ListNode(-1);
        dumpy.next=head;
        ListNode curr=dumpy;
        while (curr.next!=null){
            if (curr.next.val==val){
                curr.next=curr.next.next;
            }else {
                curr=curr.next;
            }
        }
        return dumpy.next;
    }


    // 这里是使用了虚拟头节点方法

    public ListNode removeElements(ListNode head, int val) {
        ListNode dumpy = new ListNode(-1);
        dumpy.next = head;

        ListNode curr = dumpy;
        while (curr.next != null) {
            if (curr.next.val == val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return dumpy.next;
    }

    // 那如果不是虚拟头节点
    public ListNode removeElements2(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        while (head != null && head.val == val) {
            head = head.next;
        }
        ListNode curr = head;
        // 判断时候标记是否操作空指针
        while (curr != null && curr.next != null) {
            if (curr.next.val == val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return head;
    }
}
